//
//  LeftViewController.m
//  MusicPlayer
//
//  Created by dllo on 15/5/26.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "LeftViewController.h"
#import "EntryViewController.h"
#import "NovelDataBase.h"
#import "NovelModel.h"
#import "state.h"
#import "ThemeViewController.h"

@interface LeftViewController ()


@property (nonatomic, retain) UIButton *entry;
@property (nonatomic, retain) NSArray *fiels;


@property (nonatomic, retain) UIImageView *myImageView;


@property (nonatomic, retain) UILabel *entrt;

@end

@implementation LeftViewController

- (void)dealloc
{

    [_myImageView release];

    [_entrt release];

    [_entry release];
    [_fiels release];
    [super dealloc];
}

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}



//-(void)viewWillAppear:(BOOL)animated
//{
//    
//    [super viewWillAppear:YES];
//    
//    [self.entry removeFromSuperview];
//    [self.entrt removeFromSuperview];
//    self.entry = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.height/7-20, 120, 35, 35)];
//    
//    state *single = [state shareManager];
//    NSUserDefaults *de = [NSUserDefaults standardUserDefaults];
//    single.login = [de objectForKey:@"state"];
//    if ([single.login isEqualToString:@"yes"]) {
//        [_entry setImage:[UIImage imageNamed:@"10.png"] forState:UIControlStateNormal];
//        
//        self.entrt = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.height/7-40, 160, 80, 20)];
//        self.entrt.text = @"登出";
//        self.entrt.textAlignment = 1;
//        [_entry addTarget:self action:@selector(outAction:) forControlEvents:UIControlEventTouchUpInside];
//        
//        [self.view addSubview:_entrt];
//        
//    }else{

//        [_entry setImage:[UIImage imageNamed:@"open.png"] forState:UIControlStateNormal];
//        
//        self.entrt = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.height/7-40, 160, 80, 20)];
//        _entrt.text = @"登陆/注册";
//        [self.view addSubview:_entrt];
//        
//        [_entry addTarget:self action:@selector(entryAction:) forControlEvents:UIControlEventTouchUpInside];
//    };
//    
//    
//    [self.view addSubview:_entry];
//    [_entry release];
//    
//    
//}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //使用图片初始化背景色
    UIImage *back = [UIImage imageNamed:@"PIC2.png"];
    self.view.backgroundColor = [UIColor colorWithPatternImage:back];
    //实现模糊效果
    UIVisualEffectView *visualEffectView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
    visualEffectView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) ;
    visualEffectView.alpha = 1;
    [self.view addSubview:visualEffectView];
    
    self.myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [self.view addSubview:_myImageView];
    
    [_myImageView release];
    
    [visualEffectView release];

    


    
    UIButton *backGround = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width/375*95, self.view.frame.size.height/667*100, self.view.frame.size.width/375*35,self.view.frame.size.height/667* 35)];


    [backGround setImage:[UIImage imageNamed:@"many.png"] forState:UIControlStateNormal];

    [backGround addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];

    [backGround addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    

    [self.view addSubview:backGround];
    UILabel *backLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width/375*75, self.view.frame.size.height/667*140,self.view.frame.size.width/375*80, self.view.frame.size.height/667*20)];
    backLabel.text = @"百变主题";
    [self.view addSubview:backLabel];
    
    UIButton *clean = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width/375*95, self.view.frame.size.height/667*200, 35, 35)];
    [clean setImage:[UIImage imageNamed:@"trash.png"] forState:UIControlStateNormal];
    [clean addTarget:self action:@selector(cleanAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:clean];
    UILabel *cleanLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width/375*75, self.view.frame.size.height/667*250, self.view.frame.size.width/375*80, self.view.frame.size.height/667*20)];
    cleanLabel.text = @"清理缓存";
    [self.view addSubview:cleanLabel];
    
    
    UIButton *intro = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width/375*95,self.view.frame.size.height/667* 300,self.view.frame.size.width/375* 35, self.view.frame.size.height/667*35)];
    [intro setImage:[UIImage imageNamed:@"!.png"] forState:UIControlStateNormal];
    [self.view addSubview:intro];
    
    UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width/375*75, self.view.frame.size.height/667*350,self.view.frame.size.width/375* 80, self.view.frame.size.height/667*20)];
    myLabel.text = @"关于我们";
    [self.view addSubview:myLabel];


}

- (void)cleanAction:(id)sender
{
//    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
//                   , ^{ NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES) objectAtIndex:0];
//                       
//                       self.fiels = [[NSFileManager defaultManager] subpathsAtPath:cachPath];
//                       
//                       for (NSString *p in self.fiels) {
//                           NSError *error;
//                           NSString *path = [cachPath stringByAppendingPathComponent:p];
//                           if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
//                               [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
//                           }
//                       }
//                       [self performSelectorOnMainThread:@selector(clearCacheSuccess) withObject:nil waitUntilDone:YES];});
//    
//    NSString *str = [NSString stringWithFormat:@"成功清理了 %ld kb的缓存", self.fiels.count];
//    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:str delegate:nil cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
//    [alert show];
//    
    NSFileManager *manager = [NSFileManager defaultManager];
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *path = [array lastObject];
    path = [NSString stringWithFormat:@"%@/Caches", path];
    NSLog(@"%@",path);
    BOOL judge = [manager fileExistsAtPath:path];
    if (judge) {
        [manager removeItemAtPath:path error:nil];
        //            [KVNProgress showSuccessWithStatus:@"清除成功"];
        NSString *message = @"清除成功";
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定   ", nil];
        [alert show];
        
        [alert release];
    }
    if (!judge) {
        //            [KVNProgress showErrorWithStatus:@"缓存为空"];
        NSString *message = @"缓存为空";
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定   ", nil];
        [alert show];
        [alert release];
        
    
    }
    
    
    
    
}

//-(void)clearCacheSuccess
//
//{
//    NSLog(@"清理成功");
//    
//}
//百变按钮点击事件
-(void)back:(id)sender
{
//    dd = [[UIImagePickerController alloc] init];
//    dd.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//    dd.delegate = self;
//    dd.allowsEditing = YES;
//    [self presentViewController:dd animated:YES completion:NULL];
//    NSLog(@"百变相册");

    
    NSLog(@"成功");
    
    
}


//#pragma mark -
//#pragma mark 调出相册协议
//- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
//{
//    [dd dismissViewControllerAnimated:YES completion:nil];
//    UIImage *im = [info objectForKey:UIImagePickerControllerEditedImage];
//    self.myImageView.image = im;
//   
//}
- (void)outAction:(id)sender
{
    state *single =[state shareManager ];
    if ([single.login isEqualToString:@"yes"]) {
        
        single.login = @"no";
        
        
        [[NSUserDefaults standardUserDefaults] setObject:single.login forKey:@"state"];
        
        
        UIAlertView *alet = [[UIAlertView alloc] initWithTitle:@"提示" message:@"账户已登出" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
        [alet show];
        [alet release];
    }
    [self.entry addTarget:self action:@selector(entryAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.entrt.text = @"登录/注册";
    
    
    
    
}

- (void)backAction:(id)sender
{
    ThemeViewController *entry = [[ThemeViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:entry];
    [self presentViewController:nav animated:YES completion:nil];
}

- (void)entryAction:(id)sender
{
    EntryViewController *entry = [[EntryViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:entry];
    [self presentViewController:nav animated:YES completion:nil];
    [entry release];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
